package cn.tyoui.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tyoui.bean.HgBean;
import cn.tyoui.service.HgManageService;

@Controller
public class hgController {

	@Resource
	private HgManageService hgManageService;
	
	@RequestMapping("btn_delete_hg")
	@ResponseBody
	public Map<String,Object> deleteHg(@RequestBody Map<String,Object> map){
		System.out.println("btn_delete_hg");
		String id = map.get("id").toString();
		System.out.println(id);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		try {
			isSuccess = hgManageService.deleteHg(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	@RequestMapping("change_hg")
	@ResponseBody
	public Map<String,Object> changeHg(@RequestBody HgBean hgBean){
		System.out.println("change_hg");
		System.out.println(hgBean);
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		try {
			isSuccess = hgManageService.changeHg(hgBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	@RequestMapping("query_hg")
	@ResponseBody
	public Map<String,Object> queryHg(){
		System.out.println("query_hg");
		Map<String,Object> resultMap = new HashMap<>();
		List<HgBean> hgList = new ArrayList<>();
		try {
			hgList = hgManageService.queryHg();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(hgList.size() != 0) {
			for(int i =0;i < hgList.size();i++) {
				System.out.println(hgList.get(i));
			}
		}
		resultMap.put("status", "200");
		resultMap.put("hgs", hgList);
		return resultMap;
	}
	
	@RequestMapping("add_hg")
	@ResponseBody
	public Map<String,Object> addHg(@RequestBody HgBean hgBean){
		System.out.println("add_hg");
		Map<String,Object> resultMap = new HashMap<>();
		boolean isSuccess = false;
		try {
			List<HgBean> hgList = hgManageService.queryOneHg(hgBean);
			System.out.println(hgList.size());
			if(hgList.size() == 0) {
				isSuccess = hgManageService.addHg(hgBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("status", "200");
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
}
